<template>
  <div class="swiper">
    <swiper
      :slidesPerView="3"
      :autoplay="{ delay: 300000, disableOnInteraction: true }"
      :navigation="true"
      :pagination="{ type: 'bullets', clickable: false }"
      :scrollbar="{ draggable: false }"
      :space-between="30"
      :modules="modules"
      @mouseenter="enter"
      @mouseleave="leave"
      @swiper="onSwiper"
      @slideChange="onSlideChange"
    >
      <swiper-slide
        @click="goCaseFn(item)"
        v-for="(item, index) in swiperData"
        :key="index"
        class="swiperItem"
      >
        <div>
          <img :src="$t(`messages.home.caseList[${index}].image`)" alt="" />
          <div class="imgTitle">
            <el-tooltip class="box-item" effect="dark" placement="top">
              <template #content>
                <div class="tooltipContent">
                  {{ $t(`messages.home.caseList[${index}].title`) }}
                </div>
              </template>
              <div class="title2">
                {{ $t(`messages.home.caseList[${index}].title`) }}
              </div>
            </el-tooltip>

            <div class="size">
              {{ $t(`messages.home.caseList[${index}].detils`) }}
            </div>
          </div>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script setup>
import { AutoEllipsis } from "auto-ellipsis-text"
import { homeCase } from '@/http/api/home.js'
import Zh from '@/assets/i18n/langs/zh.js'
import En from '@/assets/i18n/langs/en.js'
import { toRaw, ref, reactive, onMounted } from 'vue'
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'
import 'swiper/scss/navigation'
import 'swiper/scss/pagination'
import { useRouter } from 'vue-router'
const router = useRouter()
import { Autoplay, Navigation, Scrollbar, A11y } from 'swiper/modules'
const props = defineProps()

let modules = [Autoplay, Navigation, Scrollbar, A11y]
//定义swiperNew,目的获取非响应式swiper
let swiperNew = ''

//鼠标移入
const enter = () => {
  swiperNew.autoplay.stop()
}
//鼠标移出
const leave = () => {
  swiperNew.autoplay.start()
}
const onSwiper = (swiper) => {
  // console.log(swiper);

  swiperNew = toRaw(swiper) //拿到swiper对象再转换为非响应式
}
const onSlideChange = () => {
  // console.log("slide change");
}
let pageData = reactive({
  page: 1,
  size: 50
})
let swiperData = ref([])
const getData = async () => {
  let res = await homeCase(pageData)
  Zh.messages.home.caseList = res.res.map((item) => {
    return {
      title: item.titleZh,
      detils: item.detilsZh,
      image: item.imageZh
    }
  })
  En.messages.home.caseList = res.res.map((item) => {
    return {
      title: item.titleEn,
      detils: item.detilsEn,
      image: item.imageEn
    }
  })

  swiperData.value = res.res
}
const goCaseFn = (data) => {
  let filmNam = [{
    url: 'paint-protection-film-PPF',
    type: 'Paint Protection Film (PPF)'
  }, {
    url: 'color-paint-protection-film-PPF',
    type: 'Color Change Film'
  }, {
    url: 'window-film',
    type: 'Window Tint Film'
  }, {
    url: 'sunroof-film',
    type: 'Roof Wrap Film'
  }]
  filmNam.forEach((item) => {
    if (data.type.includes(item.type)) {
      router.push(`/caseDetils/${item.url}`)
    }
  })
}
onMounted(() => {
  getData()
})
</script>
<style lang="scss" scoped>
.tooltipContent {
  width: 500px;
}
::v-deep .swiper-button-disabled {
  display: none;
}
.swiper {
  width: 100%;
  height: 100%;
  margin: 0;
  img {
    width: 100%;
    height: 100%;
  }

  .swiperItem {
    height: 480px;
    width: 330px !important;
    cursor: pointer;
  }
}

::v-deep(.swiper-pagination-bullet) {
  //修改分页器圆点大小
  width: 8px;
  height: 8px;
  background-color: #fff;
}

.swiper-horizontal > .swiper-pagination-bullets .swiper-pagination-bullet,
::v-deep(.swiper-pagination-horizontal.swiper-pagination-bullets) .swiper-pagination-bullet {
  // 分页器远点之间的距离
  margin: 0 10px;
}

::v-deep(.swiper-button-prev),
::v-deep(.swiper-button-next) {
  color: black;
  width: 60px;
  height: 60px;
  background: #ffffff;
  border-radius: 35px;
}
.imgTitle {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  .title2 {
    height: 90px;
    font-size: 32px;
    font-weight: bold;
    color: #ffdd00;
    margin-bottom: 20px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .size {
    width: 330px;
    color: #ffffff;
    white-space: pre-wrap; /* 保留空白符序列，但是当到达容器边界时进行换行 */
    overflow-wrap: break-word; /* 在长单词或URL地址内部进行换行 */
    word-break: break-all; /* 允许在任意字符间断开（用于CJK文字等） */
  }
}

::v-deep .swiper-button-disabled {
  opacity: 1;
  pointer-events: all;
}
::v-deep .swiper-button-prev {
  position: absolute;
  left: 0px;
  top: 240px;
  opacity: 1;
  pointer-events: all;
}
::v-deep .swiper-button-next {
  position: absolute;
  right: 0px;
  top: 240px;
  opacity: 1;
  pointer-events: all;
}
</style>

